//index.js
const utils = require("../../utils/utils.js");
const biz = require("../../utils/bizUtils.js");
const listUtil = require("../../utils/listUtil.js");
const session = require("../../utils/session.js");
const toast = require("../../utils/toasts.js");
const wxm = require("../../utils/wechat_modules.js");
const com = require("../../utils/commonRequest.js");
const cache = require("../../utils/cache.js");

import {
  req
} from "../../utils/api.js";

//获取应用实例
const app = getApp()
const globalData = app.globalData;

Page({
  data: {
    motto: 'Hello World',
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    banners: ['https://img.lingxing.site/jx/banner400_3_1.png'],
    list: [],
    hidden: true,
    scrollTop: 0,
    scrollHeight: 0,
    title: '',
    keyword: null,
    catId: null,
    orderRule: 'HOT',
    waiting: true,
    share_modal: false,
    noMore: false,
    isIphoneX: globalData.isIphoneX,
    ratesall: [0, 0, 0, 0, 0],
    rates1: [0, 0, 0, 0, 0],
    rates2: [0, 0, 0, 0, 0],
    rates3: [0, 0, 0, 0, 0]
  },
  //事件处理函数
  bindViewTap: function() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onLoad: function(options) {
    var that = this;
    wx.getSystemInfo({
      success: function(res) {
        console.info(res.windowHeight);
        that.setData({
          scrollHeight: res.windowHeight
        });
      }
    });

    if (options) {
      if (options.id) {
        this.setData({
          id: options.id
        })
      }
    }

    session.login(this.refreshMe);
  },
  refreshMe: function(usr) {
    this.setData({
      user: usr
    });

    toast.showLoading();

    this.getData();
  },
  onShow: function() {
    var staff = cache.pop('staffSelect');
    if (staff) {
      this.setData({
        staff: staff
      })
    }
  },
  getData() {
    var id = this.data.id;
    var that = this;
    req('/order/detail', {
      id: id
    }).then(res => {
      // console.log(res);
      if (res.success) {
        var obj = res.result;
        obj.statusStr = biz.orderStatusStr(obj.status);
        that.setData({
          order: obj
        });
        that.getRate();
      } else {
        toast.show(res.errorMsg);
      }
    });
  },
  getRate() {
    var order = this.data.order;
    var that = this;
    req('/rate/detail', {
      orderNo: order.orderNo
    }).then(res => {
      // console.log(res);
      if (res.success) {
        var obj = res.result;
        if(!obj) {
          return;
        }

        var ratesvalall = obj.scoreall;
        var ratesall = biz.rates(obj.scoreall);

        var ratesval1 = obj.score1;
        var rates1 = biz.rates(obj.score1);

        var ratesval2 = obj.score2;
        var rates2 = biz.rates(obj.score2);

        var ratesval3 = obj.score3;
        var rates3 = biz.rates(obj.score3);

        that.setData({
          obj,
          ratesvalall,
          ratesall,
          ratesval1,
          rates1,
          ratesval2,
          rates2,
          ratesval3,
          rates3
        });
      } else {
        toast.show(res.errorMsg);
      }
    });
  },
  pushFormSubmit: function(e) {
    console.log(e);
    console.log("formId:" + e.detail.formId);
    com.saveFormId(e);
  },
  gotoMap: function() {
    var obj = this.data.obj;
    biz.gotoMap({
      title: obj.name,
      lat: obj.lat,
      lng: obj.lng,
      address: obj.addressStr
    });
  },
  tel: function() {
    wxm.makePhoneCall(this.data.obj.tel);
  },
  back: function() {
    wxm.back(this);
  },
  formSubmit: function(e) {
    var order = this.data.order;
    var form = e.detail.value;
    form.orderNo = order.orderNo;

    var ratesvalall = this.data.ratesvalall;
    if (!ratesvalall) {
      return toast.show('您还没有打分');
    }


    form.scoreall = ratesvalall;
    form.score1 = this.data.ratesval1;
    form.score2 = this.data.ratesval2;
    form.score3 = this.data.ratesval3;

    if (!form.score1 || !form.score2 || !form.score3) {
      return toast.show('请完善评分，谢谢您的支持');
    }

    toast.showLoading();
    var that = this;
    req('/rate/add', form).then(res => {
      // console.log(res);
      if (res.success) {
        toast.show('评价成功');
        that.getRate();
      } else {
        toast.show(res.errorMsg);
      }
    });
  },
  selectRate: function(e) {
    if(this.data.obj) {
      return;
    }

    var index = e.currentTarget.dataset.index;
    var type = e.currentTarget.dataset.type;

    var rates = [0, 0, 0, 0, 0];
    var ratesval = index + 1;

    for (var i = 0; i < ratesval; i++) {
      rates[i] = 1;
    }

    this.setData({
      ['rates' + type]: rates,
      ['ratesval' + type]: ratesval
    })

  }
})